package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.filechooser.FileNameExtensionFilter;

import View.ContactDeleterView;

/**
 * action implementation for the menu bar
 * @author Vincent Francolin
 * @version 1.0 : creation
 */
public class MenuBarActionListener implements ActionListener {
	ContactController ctrl;

	/**
	 * default constructor
	 * @param ctrl the main controller of the app
	 */
	public MenuBarActionListener(ContactController ctrl) {
		super();
		this.ctrl = ctrl;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		JMenuItem obj = (JMenuItem) e.getSource();
		
		String val = obj.getText();
		if(val.equals("Import a profile...")){
			System.out.println("import a profile not implemented yet");
		} else if(val.equals("Export a profile...")){
			System.out.println("export a profile not implemented yet");
		} else if(val.equals("Reset all")){
			ctrl.resetAll();
		} else if(val.equals("Quit")){
			ctrl.saveContextBeforeExit();
		} else if(val.equals("Import a contact...")){
			System.out.println("import a contact not implemented yet");
			JFileChooser winFile = new JFileChooser();
	        FileNameExtensionFilter filter = new FileNameExtensionFilter(
	                "Contact file (*.contact)", "contact");
	        winFile.setFileFilter(filter);
	        int returnVal = winFile.showOpenDialog(null);
	        if (returnVal == JFileChooser.APPROVE_OPTION) {
	            File file = winFile.getSelectedFile();
	            String filename = file.getName();
	            ctrl.addContactFromFile(file.getAbsolutePath());
	        }
		} else if(val.equals("Export a contact...")){
			JFileChooser winSave = new JFileChooser();
	        int returnValSave = winSave.showSaveDialog(null);
	        if (returnValSave == JFileChooser.APPROVE_OPTION) {
	            File file = winSave.getSelectedFile();
	            File dest = new File(file.getAbsolutePath()+".contact");
	            System.out.println(dest.getAbsolutePath());
	            if(!dest.exists()){
					try {
						dest.createNewFile();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
	            }
	            if(ctrl.getContact(ctrl.getCurrentSelected()) != null)
	            	ctrl.getContact(ctrl.getCurrentSelected()).save(dest.getAbsolutePath());
	        }
		} else if(val.equals("Delete contacts...")){
			ContactDeleterView win = new ContactDeleterView(ctrl.getContactNames(),ctrl);
			win.setVisible(true);
		} else if(val.equals("Create a group...")){
			System.out.println("create a group not implemented yet");
		} else if(val.equals("Delete groups...")){
			System.out.println("delete groups not implemented yet");
		}
	}

}
